﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;

namespace Ip_scanner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)  
        {
            IPAddress startIP;
            IPAddress endIP;

            if (IPAddress.TryParse(textBox1.Text, out startIP) && IPAddress.TryParse(textBox2.Text, out endIP))
            {
                Pinghost(IPrange(startIP, endIP));

            }
            

        }

        public IPAddress[] IPrange(IPAddress start, IPAddress end)
        {
            Byte[] ip1 = start.GetAddressBytes();
            Byte[] ip2 = end.GetAddressBytes();


            int range = (byte)(ip2[3] - ip1[3]) + 1;

            IPAddress[] rangeOfIP = new IPAddress[range];

            rangeOfIP[0] = start;

            for ( int i = 1; i < range; i++)
            {
                               
                
                ip1[3] = (byte)(ip1[3] + (byte)1);
                
                string temp = "";

                for(int i2 = 0; i2 < 4; i2++)
                {
                    temp += ip1[i2];
                    temp += ".";
                }

                temp = temp.Remove(temp.Length - 1, 1);

                rangeOfIP[i] = IPAddress.Parse(temp);
            }


            return rangeOfIP;
        }

        
                public void Pinghost(IPAddress[] pingAddress)
                {
                    Ping pinger = new Ping();

                    PingOptions options = new PingOptions();

                    options.DontFragment = true;

                    string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                    byte[] buffer = Encoding.ASCII.GetBytes(data);
                    int timeout = 120;

                    for (int i = 0; i < pingAddress.Length; i++)
                    {

                        PingReply reply = pinger.Send(pingAddress[i], timeout, buffer, options);

                        if (reply.Status == IPStatus.Success)
                        {
                            listBox1.Items.Add(pingAddress[i] + " - " + reply.Status);
                        }
                        else
                        {
                            listBox1.Items.Add(pingAddress[i] + " - " + reply.Status);
                        }

                    }
            
                }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
